jweb.util.Console = function() {
  this.maxTimes = 5;
  this.currentCount=0;
  this.appendEl = $('div#console');
  this.subscriberId=null;
  
  this.start();
}
jweb.util.Console.prototype = {
	onMessage:function(subj, msg, data) {
		if(this.currentCount>=this.maxTimes){
			$(this.appendEl).find('div:first-child').remove();
			this.currentCount = this.maxTimes;
		}
		
		if(subj == constants.midiMessage){
			var str='MIDI Message:' + '[' ;
			for(var i=0;i<msg.event.data.length;i++){
				str += msg.event.data[i].toString(10);
				if(i<msg.event.data.length-1) str +=', ';
			}
			str += ']';
			this.appendEl.append('<div>'+str+'</div>');
		}
		else if(subj == constants.midiDevice){
			var obj={title:'MIDI Device:', io:msg.ioType, manufacturer:msg.data.manufacturer};
			this.appendEl.append('<div>'+JSON.stringify(obj)+'</div>');
			window.showTip('MIDI detected: '+msg.data.manufacturer);
		}
		else this.appendEl.append('<div>'+JSON.stringify(msg)+'</div>');        this.currentCount ++;
	},
	start:function() {
		if(this.subscriberId==null) this.subscriberId='**';
		this.subscriberId = window.PageBus.subscribe('**', this, this.onMessage, null);
	},
	stop:function() {
		if(this.subscriberId!=null){
			window.PageBus.unsubscribe(this.subscriberId);
			this.subscriberId=null;
		}
	}
}